package com.example.web.service.imp;

import com.example.web.entity.Admin;
import com.example.web.exception.LoginException;
import com.example.web.mapper.AdminMapper;
import com.example.web.service.LoginService;
import javafx.fxml.LoadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.dto.ResultDTO;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    @SuppressWarnings("all")
    private AdminMapper mapper;

    @Override
    public ResultDTO login(Admin admin, HttpSession session){
        Admin dbAdmin = mapper.findByName(admin.getAdminName());
        if (dbAdmin == null){
            //return new ResultDTO(1001, "用户不存在！");
            throw new LoginException(1001, "用户不存在");
        }

        if (dbAdmin.getAdminStatus()!=null&&dbAdmin.getAdminStatus().equals(0)){
            //return new ResultDTO(1002, "该用户被锁定");
            throw new LoginException(1002, "该用户被锁定");
        }

        if (!dbAdmin.getAdminPwd().equals(admin.getAdminPwd())){
            //return new ResultDTO(1003, "密码错误！");
            throw new LoginException(1003, "密码错误！");
        }

        session.setAttribute("adminName", admin.getAdminName());
        // 改变登录时间
        dbAdmin.setLastLoginTime(new Timestamp(System.currentTimeMillis()));
        mapper.update(dbAdmin);

        return new ResultDTO(200, "登录成功");
    }
}
